package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.dao.SubjectsDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDutiesDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLDepartmentInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.DepartmentInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.transaction.annotation.Transactional;

@Component
public class AddDepartmentMutation implements DataFetcher {

    @Autowired
    OrgDepartmentsDao orgDepartmentsDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    GradesDao gradesDao;

    @Autowired
    SubjectsDao subjectsDao;

    @Autowired
    OrgDutiesDao orgDutiesDao;

    @Autowired
    TokenService tokenService;

    /**
     * 新增/修改部门添加
     * editor : ths
     */
    @Override
    @Transactional
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);

        GQLDepartmentInput input = DepartmentInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        OrgDepartments orgDepartments = null;

        if (input.getId() != null) {
            orgDepartments = orgDepartmentsDao.get(OrgDepartments.class, input.getId());
            orgDepartments.setUpdatedAt(new Date());
        } else {
            orgDepartments = new OrgDepartments();
            orgDepartments.setCreatedAt(new Date());
        }
        if (input.getDepartment_name() != null) {
            orgDepartments.setDepartmentName(JSON.toJSONString(input.getDepartment_name()));
        }
        if (input.getParent_id() != null) {
            orgDepartments.setSystemctlOrgDepartmentsByParentId(orgDepartmentsDao.get(OrgDepartments.class, input.getParent_id()));
        }
        if (input.getDepartment_order() != null) {
            orgDepartments.setDepartmentOrder(input.getDepartment_order());
        }
        if (schoolId != null) {
            orgDepartments.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class, schoolId));
        }
        if (input.getType() != null) {
            orgDepartments.setType(input.getType());
        }
        if (input.getGroup_type() != null) {
            orgDepartments.setGroupType(input.getGroup_type());
        }
        if (input.getStatus() != null) {
            orgDepartments.setStatus(input.getStatus());
        }

        if (input.getGrade() != null) {
            try {
                List<Grades> gradesList = new ArrayList<>();
                input.getGrade().forEach(g -> {
                    Grades grade = gradesDao.get(Grades.class, Long.valueOf(g.longValue()));
                    gradesList.add(grade);
                });
                orgDepartments.setGrades(gradesList);
            } catch (Exception e) {
                return Result.error(500, "" + e);
            }
        }

        if (input.getSubject() != null) {
            try {
                List<Subjects> subjectsList = new ArrayList<>();
                input.getSubject().forEach(g -> {
                    Subjects subjects = subjectsDao.get(Subjects.class, Long.valueOf(g.longValue()));
                    subjectsList.add(subjects);
                });
                orgDepartments.setSubjects(subjectsList);
            } catch (Exception e) {
                return Result.error(500, "" + e);
            }
        }

        if (input.getDuties() != null) {
            try {
                List<OrgDuties> orgDutiesList = new ArrayList<>();
                input.getDuties().forEach(g -> {
                    OrgDuties orgDuties = orgDutiesDao.get(OrgDuties.class, Long.valueOf(g.longValue()));
                    orgDutiesList.add(orgDuties);
                });
                orgDepartments.setDuties(orgDutiesList);
            } catch (Exception e) {
                return Result.error(500, "" + e);
            }
        }
        try {

            orgDepartmentsDao.saveOrUpdate(orgDepartments);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

